/*
 * ValueDocument.java
 *
 * Created on 6 de octubre de 2005, 10:25 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ig.util;
//import com.sun.java_cup.internal.version;
import java.awt.*;
import javax.swing.text.*;

/**
 *
 * @author jsanchez
 */
public class ValueDocument extends PlainDocument {

    private String tipoValor = "STRING";
    private Double mayorValor = null;
    private Double menorValor = null;
    DateVerifier dateVerifier = null;

    public ValueDocument() {
        dateVerifier = new DateVerifier();
    }

    public ValueDocument(String tipo) {
        tipoValor = tipo.trim().toUpperCase();
    }

    public void setTipoValor(String tipo) {
        tipoValor = tipo.trim().toUpperCase();
    }

    public void setMenorValor(Double menorValor) {
        if (menorValor != null) {
            this.menorValor = menorValor;
        } else {
            this.menorValor = null;
        }
    }

    public void setMayorValor(Double mayorValor) {
        if (mayorValor != null) {
            this.mayorValor = mayorValor;
        } else {
            this.mayorValor = null;
        }
    }
    private String CaracterAceptado = null;
    private Integer tamanoMaximo = null;

    public void setTamanoMaximo(Integer tamanoMaximo) {
        this.tamanoMaximo = tamanoMaximo;
    }

    public void setCaracterAceptado(String CaracterAceptado) {
        this.CaracterAceptado = CaracterAceptado;
    }

    public void insertString(int offset, String string, AttributeSet attributes) throws BadLocationException {
        dateVerifier = new DateVerifier();

        if (string == null) {
            return;
        } else {
            boolean STRING = this.dateVerifier.verify(string);
            if (STRING) {
                super.insertString(offset, string, attributes);
                return;
            }
            String newValue;
            int length = getLength();

            if (tamanoMaximo != null && tamanoMaximo <= length) {
                return;
            }
            if (length == 0) {
                newValue = string;
            } else {
                String currentContent = getText(0, length);
                StringBuffer currentBuffer = new StringBuffer(currentContent);
                currentBuffer.insert(offset, string);
                newValue = currentBuffer.toString();
            }
            try {
                if (CaracterAceptado != null) {
                    if (string.equals(CaracterAceptado)) {
                        super.insertString(offset, string, attributes);
                        return;
                    } else if (tipoValor.equals("INTEGER")) {
                        Integer.parseInt(string);
                    } else if (tipoValor.equals("LONG")) {
                        Long.parseLong(string);
                    } else if (tipoValor.equals("DOUBLE")) {
                        Double.parseDouble(string);
                    }
                    super.insertString(offset, string, attributes);
                } else {
                    if (tipoValor.equals("INTEGER")) {
                        Integer.parseInt(newValue);
                    } else if (tipoValor.equals("LONG")) {
                        Long.parseLong(newValue);
                    } else if (tipoValor.equals("DOUBLE")) {
                        Double.parseDouble(newValue);
                    }
                    if (mayorValor != null) {
                        if (Double.parseDouble(newValue) > mayorValor) {
                            throw new NumberFormatException();
                        }
                    }
                    if (menorValor != null) {
                        if (Double.parseDouble(newValue) < menorValor) {
                            throw new NumberFormatException();
                        }
                    }
                    super.insertString(offset, string, attributes);
                }
            } catch (NumberFormatException exception) {
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }
}